package com.yxy.controller;

import com.yxy.common.Result;
import com.yxy.constant.RequestMapperConstant;
import com.yxy.domain.vo.SportAllVO;
import com.yxy.domain.vo.VersionVO;
import com.yxy.service.SportService;
import com.yxy.service.VersionService;
import com.yxy.utils.QiniuOSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(RequestMapperConstant.COMMON_REQUEST_MAPPER)
@Slf4j
public class CommonController {

    private final VersionService versionService;
    private final SportService sportService;
    public CommonController(VersionService versionService, SportService sportService){
        this.versionService = versionService;
        this.sportService = sportService;
    }


    /**
     * 文件上传 七牛云
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) {
        log.info("文件名：{}", file.getOriginalFilename());
        String uploadFileName = QiniuOSSUtil.uploadToQiniu(file);
        return Result.success("上传成功", uploadFileName);
    }

    /**
     * 文件上传 七牛云
     *
     * @param file
     * @return
     */
    @PostMapping("/uploads")
    public Result<List<String>> upload(List<MultipartFile> file) {
        List<String> fileNames = new ArrayList<>();
        for (MultipartFile fileName : file) {
            log.info("文件名：{}", fileName.getOriginalFilename());
            String uploadFileName = QiniuOSSUtil.uploadToQiniu(fileName);
            fileNames.add(uploadFileName);
        }
        return Result.success("解析成功", fileNames);
    }

    /**
     * 获取版本信息
     * @return
     */
    @GetMapping("/getVersion")
    public Result<VersionVO> getVersion(){
        log.info("获取版本信息");
        VersionVO vo = versionService.getVersion();
        return Result.success("", vo);
    }

    /**
     * 获取版本列表
     * @return
     */
    @GetMapping("/list")
    public Result<List<VersionVO>> list(){
        log.info("获取版本列表");
        List<VersionVO> voList = versionService.list();
        return Result.success("", voList);
    }

    /**
     * 获取运动列表
     * @return
     */
    @GetMapping("/getSportAll")
    public Result<List<SportAllVO>> sportList(){
        log.info("获取运动列表");
        List<SportAllVO> voList = sportService.sportAll();
        return Result.success("", voList);
    }

}
